<?php

/**
 * Controller_teaching
 * 示范课教案
 */

class Controller_lesson extends MyApp {


    /** 首页 **/
    public function actionIndex()
    {

    }

    /** 示范课教案列表 **/
    public function actionLessonList ()
    {  
    	 // 分页     
        YOU::loadLib('page', "", '0');
        $tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_manage`");
        $count = $tmp['count'];
        $page_total = ceil($count/PAGE_LIMIT);
        // 当前页                  
        $p = intval($_GET['p']);
        if ($p == 0){
            $p  = 1;
        }
        if ($p > 1){	
            $start = PAGE_LIMIT * ($p-1);
        }else{
            $start = 0;
        }
		// 6=示范课教案
        $manage_list = $this->db->fetchAll("SELECT * FROM `tbl_manage` WHERE `manage_type`='6' ORDER BY manage_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach($manage_list as $key=>$value){
			// 教师名称（提交者）
			$staff_data = $this->db->fetchRow("SELECT `staff_name` FROM `tbl_staff` WHERE staff_id={$value['staff_id']}");
			$manage_list[$key]['staff_name'] = $staff_data['staff_name'];
		}
		// 搜索
		if($_POST){

			$manage_title     		= $this->post->manage_title;
			$start_time     		= $this->post->start_time;
			$end_time     			= $this->post->end_time;

			// 标题搜索
			if(!empty($manage_title)){
				
				$manage_list = $this->db->fetchAll("SELECT * FROM `tbl_manage` WHERE manage_title LIKE '%{$manage_title}%' AND `manage_type`='6'");
	
			}
			
			// 时间段搜索
			if(!empty($start_time) && !empty($end_time) ){
				
				$manage_list = $this->db->fetchAll("SELECT * FROM `tbl_manage` WHERE `manage_type`='6' AND create_time>='{$start_time}' AND create_time<='{$end_time}'");
			
			}

		}

        $page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
        $page->next_page   = '下一页>';
        $page->pre_page    = '<上一页';
        $page->style_path  = "static/page_style";
       
	    $this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("manage_list", $manage_list);
        $this->tpl->display('lesson/list.tpl.php');
    }
	/** 教学工作手册添加 **/
    public function actionlessonAdd()
    {        

        if ($_POST){ 

            $manage_title     	    = $this->post->manage_title;
            $manage_content   		= $this->post->manage_content;             

            // 写入数据库
            $info = array( 
				
			"manage_title"  	    => $manage_title,
            "manage_content"       	=> $manage_content,
			// 示范课教案 =6
			"manage_type"       	=> 5,	
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  

			 );

          $this->db->do_insert("tbl_manage", $info);
          Helper_Client::location("添加成功", "index.php?a=lesson.lessonlist");

         }
          
         $this->tpl->display("lesson/add.tpl.php");
    }
    /** 教学工作手册修改 **/
    public function actionLessonUpdate()
    {   

        $manage_id   = (int)$this->get->manage_id;
        $manage_data = $this->db->fetchRow("SELECT * FROM `tbl_manage` WHERE `manage_id`='{$manage_id}'");

	    if (empty($manage_data)){
            Helper_Client::alert("示范课教案不存在");
        }

        if ($_POST){ 

            $manage_title     	    = $this->post->manage_title;
            $manage_content   		= $this->post->manage_content;    
                  

            // 写入数据库
            $info = array(
				
			"manage_title"  	    => $manage_title,
            "manage_content"       	=> $manage_content,                      
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",  

			 );

          $this->db->do_update("tbl_manage", $info, "manage_id='{$manage_id}'");

          // 跳转
          Helper_Client::location("修改成功", "index.php?a=lesson.lessonlist");

         }

            $this->tpl->assign("manage_data", $manage_data);
	        $this->tpl->display("lesson/update.tpl.php");
    } 
	/** 教学工作手册删除 **/
    public function actionlessonDel()
    {
        $manage_id = $this->get->manage_id;   
        $this->db->query("DELETE  FROM `tbl_manage` WHERE manage_id='{$manage_id}'");
         // 跳转
        Helper_Client::location("删除成功", "index.php?a=lesson.lessonlist");
    }
	

}

?>